package com.example.server.service;


import com.example.server.domain.Users;
import com.example.server.mapper.UserMapper;
import com.example.server.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class UserService {

    private UserMapper userMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    public UserService(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
    public String login(String username, String password) {
        Users user = userMapper.login(username, password);
        if(user == null){
            return "登录失败";
        }
        String token = JWTUtils.generateToken(username);

        //登录成功后，把token放入redis，并且设置一下超时时间
        redisTemplate.opsForValue().set(token, username, 86400,TimeUnit.SECONDS);

        return token;


    }
}
